/**
 * PageMarket
 * MyApplication.java
 * com.android.pagemarket
 * 
 */
package com.android.pagemarket;

import java.util.Date;

import android.app.Application;
import android.content.Context;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.os.Handler;
import android.os.Process;
import android.preference.PreferenceManager;

import com.android.pagemarket.business.logic.BaseManager;
import com.android.pagemarket.business.logic.ContentManager;
import com.android.pagemarket.business.logic.InitManager;
import com.android.pagemarket.business.update.UpdateManager;
import com.android.pagemarket.io.database.DB;
import com.android.pagemarket.ui.service.NetworkStateReceiver;
import com.android.pagemarket.util.ImageLoader;
import com.android.pagemarket.util.Util;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-8-6
 * @see : 应用上下文
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class MyApplication extends Application 
{
    
    private static MyApplication instance;
    private NetworkStateReceiver networkStateReceiver;
    public Handler mHandler;
    public SharedPreferences mPreference;
    /** 是否是第一次*/
    public boolean isFirst = true;
    /** 是否弹出过快捷提示*/
    public boolean isShortCutTip = false;
    /** 是否是最新版本*/
    public boolean isLastVer = false;
    /** 跳过视频的次数*/
    public int passVideoCount;
    /** 访问首页的次数*/
    public int visitStartCount;
    /** 是否指导过*/
    public boolean isDirect = false;
    
    @Override
    public void onCreate()
    {
        super.onCreate();
        instance = this;
//        startCheckNetWork();
        mHandler = new Handler();
        mPreference = PreferenceManager.getDefaultSharedPreferences(this);
        ImageLoader.init(this, 0, Util.getExternDir("/pagemarket/img/"));
        DB.initDB(this);
        BaseManager.initAll();
        restore();
    }
    
    @Override
    public void onTerminate()
    {
        exit();
        super.onTerminate();
    }
    
    public void exit()
    {
        UpdateManager.getInstacnce(this).destory();
        DB.closeDB();
//        stopCheckNetWork();
        Process.killProcess(Process.myPid());        
    }
    
    public static MyApplication getContext()
    {
        return instance;
    }

    public boolean isNetWorkAvailable()
    {
        ConnectivityManager connectMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        if(connectMgr.getActiveNetworkInfo() != null)
        {
            return connectMgr.getActiveNetworkInfo().isAvailable();
        }else
        {
            return false;
        }
    }    
        
    public void startCheckNetWork()
    {
        if(networkStateReceiver == null)
        {
            networkStateReceiver = new NetworkStateReceiver();
            registerReceiver(networkStateReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
        }
    }
    
    public void stopCheckNetWork()
    {
        if(networkStateReceiver != null)
        {
            unregisterReceiver(networkStateReceiver);
            networkStateReceiver = null;
        }
    }
    
    // 是否快捷开关
    public boolean isShortCut()
    {
        return mPreference.getBoolean("short_cut", false);
    }    
    // 设置快捷开关
    public void setShortCut(boolean have)
    {
        mPreference.edit().putBoolean("short_cut", have).commit();
    }
    
    /** 是否显示快捷提示*/
    public boolean isShowShortCut()
    {
        return !isShortCutTip && !isShortCut();
    }
    
    // 恢复配置值
    private void restore()
    {
        passVideoCount = mPreference.getInt("passVideoCount", 0);
        visitStartCount = mPreference.getInt("visitStartCount", 0);
        isDirect = mPreference.getBoolean("isDirect", true);
        InitManager.getInstance().autoLogin();
    }
    
    public void addVideoCount()
    {
        mPreference.edit().putInt("passVideoCount", passVideoCount + 1).commit();
    }
    
    public boolean isShowVideo()
    {
        return passVideoCount < 3;
    }
    
    public void addVisitStartCount()
    {
        mPreference.edit().putInt("visitStartCount", visitStartCount + 1).commit();
        visitStartCount +=1;
    }
    
    public boolean isShowTipLogin()
    {
        return visitStartCount > 3;
    }
    
    public void resetStartCount()
    {
        mPreference.edit().putInt("visitStartCount", 0).commit();
        visitStartCount = 0;
    }
    
    public boolean isPushOn(boolean pushMsg, boolean pushOut, boolean pushQuot)
    {
        if(pushMsg) return mPreference.getBoolean("isPushMsg", true);
        if(pushOut) return mPreference.getBoolean("isPushOut", true);
        if(pushQuot) return mPreference.getBoolean("isPushQuot", true);
        return false;
    }
    
    public boolean isPushOn()
    {
        return isPushOn(true, false, false) || isPushOn(false, true, false) || isPushOn(false, false, true);
    }
    
    public void setPushOn(Boolean pushMsg, Boolean pushOut, Boolean pushQuot)
    {
        if(pushMsg != null) mPreference.edit().putBoolean("isPushMsg", pushMsg).commit();
        if(pushOut != null) mPreference.edit().putBoolean("isPushOut", pushOut).commit();
        if(pushQuot != null) mPreference.edit().putBoolean("isPushQuot", pushQuot).commit();
        
        if(isPushOn())
        {
            ContentManager.getInstance().startPush();
        }else
        {
            ContentManager.getInstance().stopPush();
        }
    }

    public String getLastLoginName()
    {
        return mPreference.getString("lastLoginName", null);
    }
    
    public String getLastLoginNum()
    {
        return mPreference.getString("lastLoginNum", null);
    }
    
    public void setLastLogin(String name, String num)
    {
        if(name != null)
        {
            mPreference.edit().putString("lastLoginName", name).commit();
        }else
        {
            mPreference.edit().remove("lastLoginName").commit();
        }
        
        if(num != null)
        {
            mPreference.edit().putString("lastLoginNum", num).commit();
        }else
        {
            mPreference.edit().remove("lastLoginNum").commit();
        }
    }
    
    public void setQuotPushRecord(String time)
    {
        mPreference.edit().putString("quotPushed", time).commit();
    }
    
    public boolean isQuotPushed()
    {
        String record = mPreference.getString("quotPushed", null);
        if(record != null)
        {
            String now = InitManager.formater.format(new Date());
            if(now.equals(record))
            {
                // 相同，即已经获取过 这个推送， 不再显示
                return true;
            }else
            {
                return false;
            }
        }else
        {
            return false;
        }
    }
    
    /**
    public void setOrderPushRecord(String time)
    {
        mPreference.edit().putString("orderPushed", time).commit();
    }
    
    public boolean isOrderPushed()
    {
        String record = mPreference.getString("orderPushed", null);
        if(record != null)
        {
            String now = InitManager.formater.format(new Date());
            if(now.equals(record))
            {
                // 相同，即已经获取过 这个推送， 不再显示
                return true;
            }else
            {
                return false;
            }
        }else
        {
            return false;
        }
    }*/
    
    public boolean isDireactable()
    {
        return isDirect;
    }
    
    public void setDireactable()
    {
        isDirect = false;
        mPreference.edit().putBoolean("isDirect", false).commit();
    }
}